﻿using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.BomCOmmand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.BomConnamdHandler
{
    /// <summary>
    /// 查询物料信息
    /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
    /// </summary>
    public class GetBomPartsCommandHandler : IRequestHandler<GetBomPartsCommand, APIResult<List<BomParts>>>
    {
        private readonly IBaseRepository<BomParts> partsRep;
        public GetBomPartsCommandHandler(IBaseRepository<BomParts> partsRep)
        {
            this.partsRep = partsRep;
        }
        public Task<APIResult<List<BomParts>>> Handle(GetBomPartsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BomParts>> result = new APIResult<List<BomParts>>();

            var list = partsRep.GetAll().Result;
            if (!string.IsNullOrEmpty(request.BomCode))
            {
                list = list.Where(x => x.BomCode == request.BomCode);
            }
            if (!string.IsNullOrEmpty(request.PartsName))
            {
                list = list.Where(x => x.PartsName.Contains(request.PartsName));
            }
            list = list.OrderByDescending(x => x.PartsId);
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
